<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: GetCircle.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\note;

use app\api\model\dao\Note;
use app\api\model\dao\NoteReply;
use app\api\model\dao\NoteReplyPraise;
use app\api\model\dao\NoteReplyReply;
use app\api\model\dao\UserAccount;
use app\api\model\data\note\NoteData;
use app\library\CommonConfig;
use app\library\Error;
use app\library\Flag;
use app\library\PageBase;
use think\Exception;
use think\Validate;

class GetNoteReplyReplyList extends PageBase
{

    private $objNote;
    private $objNoteReply;
    private $objNoteReplyReply;
    private $objNoteReplyPraise;
    private $objUserAccount;
    private $objNoteData;

    public function __construct()
    {
        parent::__construct();

        $this->objNote = new Note();
        $this->objNoteReply = new NoteReply();
        $this->objUserAccount = new UserAccount();
        $this->objNoteReplyReply = new NoteReplyReply();
        $this->objNoteReplyPraise = new NoteReplyPraise();
        $this->objNoteData = new NoteData();
    }

    /**
     * page 入口
     *
     * @param $param
     * @return array
     * @throws Exception
     */
    public function excute($param)
    {
        $ret = $this->checkToken($param);

        $this->user_id = !empty($this->user_id) ? $this->user_id : 0;

        $this->data['list'] = [];

        $conds = [
            'recv_userid' => ['=', $this->user_id],
            //'status' => ['=', 0],
        ];
        $replyreply_ret = $this->objNoteReplyReply->getList($conds);
        if (empty($replyreply_ret)) {
            return $this->getRes();
        }

        $replyid = [];
        $replyid2Reply = [];

        foreach ($replyreply_ret as $val) {
            $replyid[] = $val['reply_id'];
            if (!empty($replyid2Reply[$val['reply_id']])) {
                if ($val['create_time'] > $replyid2Reply[$val['reply_id']]['create_time']) {
                    $replyid2Reply[$val['reply_id']] = $val;
                }
            } else {
                $replyid2Reply[$val['reply_id']] = $val;
            }
        }

        $noteReplay = $this->getNoteReplayFromDB($this->user_id, $replyid);
        if (empty($noteReplay)) {
            return $this->getRes();
        }

        $userid = [];
        $user_account = [];


        foreach ($noteReplay as $val) {
            $userid[] = $val['userid'];
        }

        if (!empty($userid)) {
            $userid = array_unique($userid);
            $user_ret = $this->objUserAccount->getUserAccountByUserids($userid);
            if (!empty($user_ret)) {
                $user_ret = $this->modelToArray($user_ret);
                foreach ($user_ret as $val) {
                    $user_account[$val['id']] = $val;
                }
            }
        }

        $this->data['list'] = $this->composeData($noteReplay, $user_account, $replyreply_ret);

        return $this->getRes();
    }

    /**
     * 从数据库中获取帖子回复列表
     *
     * @param $cir_id
     * @return bool|false|\PDOStatement|string|\think\Collection
     */
    public function getNoteReplayFromDB($user_id, $replyids)
    {
        if (!$user_id) {
            return false;
        }
        $arrCond = [
            'id' => ['in', $replyids],
            'userid' => ['=', $user_id],
            'is_delete' => ['=', Flag::CONST_NOTEREPLAY_DELETE_NOT]
        ];
        $ret = $this->objNoteReply->getNoteReply($arrCond, [], 0, 10000, ' create_time desc ');
        if (false === $ret) {
            return [];
        }
        if (empty($ret)) {
            return [];
        }
        return $ret;
    }

    public function composeData($note_replay, $user_account, $replyToreply)
    {
        $data = [];
        foreach ($note_replay as $val) {
            if (empty($user_account[$val['userid']])) {
                continue;
            }
            $nickname = $user_account[$val['userid']]['nick_name'] ? $user_account[$val['userid']]['nick_name'] : '踏着尸体前行';
            $photo = $user_account[$val['userid']]['avatar'] ? $user_account[$val['userid']]['avatar'] : CommonConfig::DEFAULT_AVATAR;
            $data[] = [
                'note_id' => $val['note_id'],
                'reply_id' => $val['id'],
                'userid' => $val['userid'],
                'nickname' => $nickname,
                'photo' => $photo,
                'content' => $val['content'],
                'time' => $this->objNoteData->caleDate($val['create_time']),
                'reply' => !empty($replyToreply[$val['id']]['content']) ? $replyToreply[$val['id']]['content'] : '',
            ];
        }
        return $data;
    }
}
